import 'package:flutter/material.dart';

class WCheckButton extends StatefulWidget {
  final double height;
  final double width;
  final double borderRadius;
  final Color normalColor;
  final Color checkedColor;
  final Color borderColor;

  final Widget normalWidget;
  final Widget checkedWidget;

  final Function onTap;

  final bool value;

  WCheckButton(
      {this.height = 15,
      this.width = 15,
      this.borderRadius = 3,
      this.normalColor,
      this.checkedColor,
      this.borderColor,
      this.normalWidget,
      this.checkedWidget,
      this.onTap,
      @required this.value});

  @override
  State<StatefulWidget> createState() {
    return _WCheckButtonState();
  }
}

class _WCheckButtonState extends State<WCheckButton> {
  bool _stateValue;

  @override
  Widget build(BuildContext context) {
    _stateValue ??= widget.value;
    Widget body;

    if (widget.checkedWidget != null && widget.normalWidget != null) {
      body = _stateValue ? widget.checkedWidget : widget.normalWidget;
    } else {
      body = Container(
        height: widget.height,
        width: widget.width,
        decoration: BoxDecoration(
            color: _stateValue
                ? widget.checkedColor ?? Theme.of(context).primaryColor
                : widget.normalColor ?? Colors.grey[100],
            borderRadius: BorderRadius.circular(widget.borderRadius),
            border: Border.all(
                color: widget.borderColor ?? Colors.grey, width: 0.5)),
      );
    }

    return Container(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _stateValue = !_stateValue;
            widget.onTap?.call(_stateValue);
          });
        },
        child: body,
      ),
    );
  }
}
